import json

from flask import Flask, request, render_template, make_response, jsonify

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def index():
    user = {'nickname': 'Miguel'}
    return render_template("index.html", title='Home', user=user)


@app.route('/json', methods=['GET'])
def json_send():
    json_str = {"name": "zhangyu", "year": 23}
    result_json = json.dumps(json_str)
    resp = make_response(result_json)
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp


@app.route('/signin', methods=['GET'])
def signin_form():
    return '''<form action="/signin" method="post">
              <p><input name="username"></p>
              <p><input name="password" type="password"></p>
              <p><button type="submit">Sign In</button></p>
              </form>'''


@app.route('/signin', methods=['POST'])
def signin():
    if request.form['username'] == 'admin' and request.form['password'] == 'password':
        return '<h3>hello!admin!</h3>'
    return '<h3>Bad username or password.</h3>'


if __name__ == '__main__':
    app.run(
        host='0.0.0.0',
        port=8080,
        debug=True
    )
